package com.firewolf.springcloud;

import com.firewolf.springcloud.pojo.User;
import com.firewolf.springcloud.vo.ResponseVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 作者：刘兴
 * 时间:19/2/15
 **/
@Service
public class UserService {

    @Autowired
    private RestTemplate restTemplate;

    //指定当请求失败的时候调用的方法
    @HystrixCommand(fallbackMethod = "addUserFail")
    public ResponseVO addUser(User u){
        ResponseVO responseVO = restTemplate.postForObject("http://provider/user", u, ResponseVO.class);
        return  responseVO;
    }

    /**
     * 错误回调方法和本身发起请求的方法返回类型保持一致
     * @return
     */
    public ResponseVO addUserFail(User u){
        return ResponseVO.error("请求失败!!!");
    }
}
